package com.vincent.sciinfo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vincent.sciinfo.pojo.Paper;
import com.vincent.sciinfo.pojo.Source;
import com.vincent.sciinfo.pojo.TSMResult;
import com.vincent.sciinfo.service.PaperService;
import com.vincent.sciinfo.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperServer;

    @Autowired
    private SourceService sourceServer;

    @RequestMapping("/show_paper")
    public PageInfo showPaper(
            @RequestParam("Id") String Id, @RequestParam("ope") int ope,
            @RequestParam(value = "pn", defaultValue = "1") int pn,
            @RequestParam(value = "pageSize") int pageSize) {
        PageHelper.startPage(pn, pageSize);
        List<Paper> list = paperServer.selectPaperByTeacherId(ope, Id);
        PageInfo pageInfo = new PageInfo(list, 5);
        return pageInfo;
    }

    @RequestMapping("/add_paper")
    public int insertPaper(@RequestBody(required = false) Paper paper) {
        return paperServer.insertPaper(paper);
    }

    @RequestMapping("/selectCheckAndRemovePaper")
    public PageInfo selectCheckAndRemovePaper(@RequestParam Integer pn, @RequestParam Integer pageSize) {
        PageHelper.startPage(pn, pageSize);
        List<Paper> list = paperServer.selectCheckAndRemovePaper();
        for (Paper paper : list) {
            Source source = sourceServer.isExistsPaperFile(paper.getPapername() + ".doc");
            if(source != null) paper.setIsDownloadAllowed(1);
        }
        PageInfo page = new PageInfo(list, pageSize);
        return page;
    }

    @RequestMapping("/updatePaperStatus")
    public void updatePaperStatus(@RequestParam String paperId, @RequestParam int operaCode) {
        paperServer.updatePaperStatus(paperId, operaCode);
    }

    @RequestMapping("/selectPaperNameById")
    public String[] selectPaperNameById(@RequestParam String paperId) {
        return paperServer.selectPaperNameById(paperId);
    }
}
